from typing import Union
from utils import Color
from filters.manager import FilterManager


@FilterManager.register
def join(sep: str, array: list):
    return sep.join(array)


@FilterManager.register
def first(array: list):
    if array:
        return array[0]
    return ""


@FilterManager.register
def last(array: list):
    if array:
        return array[-1]
    return ""


@FilterManager.register
def concat(other_array: list, array: list):
    array.extend(other_array)
    return "".join(array)


# NOTE: Index Filter doesn't need implement here
# NOTE: it will be implemented as variable


@FilterManager.register
def map(key: str, array: list):
    if array:
        return "".join(map(lambda item: item.get(key), array))
    return ""


@FilterManager.register
def reverse(array: list):
    return "".join(array[::-1])


@FilterManager.register
def size(array: list):
    return len(array)


@FilterManager.register
def sort(key: str, array: list):
    return array.sort(key=lambda item: item.get(key))


@FilterManager.register
def where(required_keys: list, array: list):
    return filter(lambda item: all([item.get(key) for key in required_keys]), array)


@FilterManager.register
def uniq(array: list):
    return list(set(array))